package com.eascs.o2o.message.controller;


import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.o2o.common.vo.ModelResult;
import com.eascs.o2o.message.server.IMsgSmsService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;

@RequestMapping("/msg/rest")
@RestController
public class MsgSmsRestController extends BaseRestController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private static final String DX_VERIFY_TEMPLATE_CODE = "DX-803095618";// [怡亚通]金融平台验证码{0}

	@Autowired
	private IMsgSmsService msgSmsService;
	
	@RequestMapping(value = "/sendAuthCode")
	@ResponseBody
	public BaseRestResult<ModelResult<Boolean>> sendAuthCode(HttpServletRequest request, String phone) {
		ModelResult<Boolean> modelResult = new ModelResult<>();
		if (StringUtils.isBlank(phone)) {
			modelResult.withError("sendAuthCode error", "参数为空");
			return BaseRestResult.buildRestResult(modelResult);
		}
		try {
			modelResult = msgSmsService.sendAuthCode(DX_VERIFY_TEMPLATE_CODE, phone);
			return BaseRestResult.buildRestResult(modelResult);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("sendAuthCode error:[{}],errorMsg:[{}]",e, e.getMessage());
			modelResult.withError("sendAuthCode error", "获取短信验证码报错，请联系管理员");
			return BaseRestResult.buildRestResult(modelResult);
		}
	}

	@RequestMapping(value = "/checkAuthCode")
	@ResponseBody
	public BaseRestResult<ModelResult<Boolean>> checkAuthCode(HttpServletRequest request, String phone,
			String authCode) {
		ModelResult<Boolean> modelResult = new ModelResult<>();
		if (StringUtils.isBlank(phone) || StringUtils.isBlank(authCode)) {
			modelResult.withError("checkAuthCode error", "参数为空");
			return BaseRestResult.buildRestResult(modelResult);
		}
		try {

			modelResult = msgSmsService.useAuthCode(phone, authCode);
			return BaseRestResult.buildRestResult(modelResult);
		} catch (Exception e) {
			logger.info("checkAuthCode error[{}]", e.getMessage());
			modelResult.withError("checkAuthCode error", "校验短信证码报错，请联系管理员");
			return BaseRestResult.buildRestResult(modelResult);
		}
	}

}
